package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbSpecialProcessData;
import com.sailfish.springbootdemo.pojo.db2.PcbSpecialProcessInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbSpecialProcessDataDao {

    @Results(id = "pcbSpecialProcessDataMap", value = {
            @Result(column = "pcb_special_process_data_id", property = "pcbSpecialProcessDataId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_special_process_info_id", property = "pcbSpecialProcessInfoId", jdbcType = JdbcType.INTEGER),
            @Result(column = "factory_id", property = "factoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "satisfied", property = "satisfied", jdbcType = JdbcType.INTEGER),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "version", property = "version", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
    })

    /*
     *查询单个
     */
    @Select("<script> " +
            "select pcb_special_process_data_id, pcb_special_process_info_id, factory_id, satisfied, remark, version, create_time, create_user, update_time, update_user, status, sort" +
            " from " + DB2TableName.PCB_SPECIAL_PROCESS_DATA +
            " where pcb_special_process_data_id = #{id}" +
            "</script>")
    PcbSpecialProcessData getById(@Param("id") Integer id);

    @Select("<script> " +
            "select pcb_special_process_data_id, pcb_special_process_info_id, factory_id, satisfied, remark, version, create_time, create_user, update_time, update_user, status, sort" +
            " from " + DB2TableName.PCB_SPECIAL_PROCESS_DATA +
            " where 1=1 " +
            "<if test = 'pcbSpecialProcessInfoId != null'>" +
            " and pcb_special_process_info_id = #{pcbSpecialProcessInfoId}" +
            "</if>" +
            "<if test = 'factoryId != null'>" +
            " and factory_id = #{factoryId}" +
            "</if>" +
            "<if test = 'version != null'>" +
            " and version = #{version}" +
            "</if>" +
            "<if test='isStatisfied != null' >" +
            "and satisfied = #{isStatisfied}" +
            "</if>" +
            " order by sort" +
            "</script>")
    @ResultMap(value = "pcbSpecialProcessDataMap")
    List<PcbSpecialProcessData> getAllByLimit(@Param("pcbSpecialProcessInfoId") Integer pcbSpecialProcessInfoId,
                                              @Param("factoryId") Integer factoryId,
                                              @Param("version") String version,
                                              @Param("isStatisfied") Integer isStatisfied);

    /*
     *按工厂查询
     */
    @Select("<script> " +
            "select pcb_special_process_data_id, pcb_special_process_info_id, factory_id, satisfied, remark, version, create_time, create_user, update_time, update_user, status, sort " +
            " from " + DB2TableName.PCB_SPECIAL_PROCESS_DATA +
            "<where>" +
            "<if test='factoryId != null'>" +
            "and factory_id = #{factoryId}" +
            "</if>" +
            "<if test='status != null'>" +
            "and status = #{status}" +
            "</if>" +
            "</where>" +
            " order by sort" +
            "</script>")
    List<PcbSpecialProcessData> getAllByFactoryId(Integer factoryId);

    @Insert("insert into " + DB2TableName.PCB_SPECIAL_PROCESS_DATA + "(pcb_special_process_info_id, factory_id, satisfied, remark, version, create_time, create_user, update_time, update_user, status, sort) values" +
            " (#{pcbSpecialProcessInfoId}, #{factoryId}, #{satisfied}, #{remark}, #{version}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort})")
    // 显式设置主键
    @Options(useGeneratedKeys = true, keyProperty = "pcbSpecialProcessDataId")
    Integer insert(PcbSpecialProcessData param);

    /*
     *通过主键删除
     */
    @Delete("delete from " + DB2TableName.PCB_SPECIAL_PROCESS_DATA + " where pcb_special_process_data_id = #{id} ")
    Integer delete(@Param("id") Integer id);

    /*
     *通过主键修改数据
     */
    @Update("<script>" +
            "update " + DB2TableName.PCB_SPECIAL_PROCESS_DATA +
            "<set> " +
            "<if test='pcbSpecialProcessInfoId != null and pcbSpecialProcessInfoId != \"\"'>" +
            "pcb_special_process_info_id = #{pcbSpecialProcessInfoId}," +
            "</if>" +
            "<if test='factoryId != null and factoryId != \"\"'>" +
            "factoryId = #{factoryId}," +
            "</if>" +
            "<if test='satisfied != null'>" +
            "satisfied = #{satisfied}," +
            "</if>" +
            "<if test='remark != null and remark != \"\"'>" +
            "remark = #{remark}," +
            "</if>" +
            "<if test='version != null and version != \"\"'>" +
            "version = #{version}," +
            "</if>" +
            "<if test='createUser != null'>" +
            "create_user = #{createUser}," +
            "</if>" +
            "<if test='createTime != null '>" +
            "create_time = #{createTime}," +
            "</if>" +
            "<if test='updateUser != null'>" +
            "update_user =#{updateUser}," +
            "</if>" +
            "<if test='updateTime != null '>" +
            "update_time = #{updateTime}," +
            "</if>" +
            "<if test='status != null '>" +
            "status = #{status}," +
            "</if>" +
            "<if test='sort != null '>" +
            "sort = #{sort} " +
            "</if>" +
            "</set> " +
            "where pcb_special_process_data_id = #{pcbSpecialProcessDataId}" +
            "</script>")
    Integer update(PcbSpecialProcessData param);

    /*
     *通过组合key
     */
    @Update("<script>" +
            "update " + DB2TableName.PCB_SPECIAL_PROCESS_DATA +
            "<set> " +
            "<if test='satisfied != null'>" +
            "satisfied = #{satisfied}," +
            "</if>" +
            "<if test='remark != null and remark != \"\"'>" +
            "remark = #{remark}," +
            "</if>" +
            "<if test='version != null and version != \"\"'>" +
            "version = #{version}," +
            "</if>" +
            "<if test='createUser != null'>" +
            "create_user = #{createUser}," +
            "</if>" +
            "<if test='createTime != null '>" +
            "create_time = #{createTime}," +
            "</if>" +
            "<if test='updateUser != null'>" +
            "update_user =#{updateUser}," +
            "</if>" +
            "<if test='updateTime != null '>" +
            "update_time = #{updateTime}," +
            "</if>" +
            "<if test='status != null '>" +
            "status = #{status}," +
            "</if>" +
            "<if test='sort != null '>" +
            "sort = #{sort} " +
            "</if>" +
            "</set> " +
            "where factory_id = #{factoryId} and pcb_special_process_info_id = #{pcbSpecialProcessInfoId} " +
            "</script>")
    Integer updateByFacIdAndInfoId(PcbSpecialProcessData param);
}
